/*
 * IndovinaTraccia.java
 *
 * Created on 30 aprile 2007, 18.35
 */

package alecava;

import alecava.interfacce.Progressable;
import java.util.*;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  alecava
 */
public class IndovinaTraccia extends javax.swing.JFrame implements Progressable {
    private Collection<AbstractCavaID3> c;
    /** Creates new form IndovinaTraccia */
    public IndovinaTraccia(Collection<AbstractCavaID3> c) {
        initComponents();
        this.setVisible(true);
        this.c = c;
        popolaTabella();
        estraiTracce();
        /*cazzata*/
        Timer t = new Timer();
        t.schedule(new ChangeColor(this.table), 0, 20);
        /*******************/
    }
    
    private void estraiTracce(){
        DefaultTableModel model = (DefaultTableModel) this.table.getModel();
        ID3FileNameHandler handler = new ID3FileNameHandler();
        int i = 0;
        int failed = 0;
        for (AbstractCavaID3 id : c){
            String traccia = handler.extractTrack(id.getFile().getName());
            model.setValueAt(traccia , i++, 1);
            if (traccia.equals(""))
                failed++;
        }
        if (failed > 0)
            labelInfo.setText("Non è stato possibile indovinare " + failed + " tracce");
    }
    
    private void popolaTabella(){
        DefaultTableModel model = (DefaultTableModel) this.table.getModel();
        model.setRowCount(c.size());
        int i = 0;
        for (AbstractCavaID3 id : c){
            model.setValueAt(id , i++, 0);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        btnSalva = new javax.swing.JButton();
        jProgressBar1 = new javax.swing.JProgressBar();
        labelInfo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Indovina traccia");
        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null}
            },
            new String [] {
                "Files", "Traccia"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(table);

        btnSalva.setText("Scrivi tags");
        btnSalva.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnSalvaMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 540, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 540, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(labelInfo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 442, Short.MAX_VALUE)
                        .addComponent(btnSalva)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalva)
                    .addComponent(labelInfo))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalvaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnSalvaMouseClicked
// TODO add your handling code here:
        AbstractCavaID3[] ids = new AbstractCavaID3[c.size()];
        int ind = 0;
        /*voglio copiare i riferimenti in modo che anche nel form principale si vedono
         i cambiamenti senza rifare la ricerca*/
        for (AbstractCavaID3 x : c){
            ids[ind++] = x;
        }
        /*contruisce la tabella con i campi da scrivere*/
        String[][] fields = new String[ids.length][7];
        for (int i = 0; i < ids.length; i++){
            fields[i][0] = ids[i].getArtist();
            fields[i][1] = ids[i].getTitle();
            fields[i][2] = ids[i].getAlbum();
            fields[i][3] = ids[i].getComment();
            fields[i][4] = ids[i].getYear();
            fields[i][5] = ids[i].getGenre();
            fields[i][6] = (String) table.getModel().getValueAt(i, 1);
        }
        /*scrive*/
        Thread w = new MassWriter(ids, fields, this);
        w.start();
    }//GEN-LAST:event_btnSalvaMouseClicked

    @Override
    public void start() {
        this.jProgressBar1.setStringPainted(true);
        this.jProgressBar1.setValue(0);
    }

    @Override
    public void finish() {
        //this.jProgressBar1.setValue(0);
    }

    @Override
    public void setMax(int x) {
        this.jProgressBar1.setMinimum(0);
        this.jProgressBar1.setMaximum(x-1);
    }

    @Override
    public void setPosition(int x) {
        this.jProgressBar1.setValue(x);
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSalva;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelInfo;
    private javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables
    
}
